﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports DevComponents.DotNetBar

Public Class frmPersonal

    Dim variableconexion1 As New SqlConnection(My.Settings.CITRA_ERPConnectionString)
    Dim variableconexion2 As New SqlConnection(My.Settings.ctCITRAAUSL2012ConnectionString)


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        If ModGeneral.EstadoRed() = False Then
            StatusPersona.Visible = True
        Else
            StatusPersona.Visible = False
        End If

    End Sub

    Private Sub btnAtrasPersonas_Click(sender As Object, e As EventArgs) Handles btnAtrasPersonas.Click
        Me.Close()
    End Sub

    Public Sub llenarCombo()

        Dim ds As New DataSet
        Dim da As New SqlDataAdapter("select idEmpleado, NombreCompleto from empleados Order by NombreCompleto;" & "select idEmpleado, NombreCompleto from empleados Order by NombreCompleto;", variableconexion2)
        da.Fill(ds)

        cmbEmpleado.DataSource = ds.Tables(0)
        cmbEmpleado.DisplayMember = "NombreCompleto"
        cmbEmpleado.ValueMember = "idEmpleado"

        cmbSuperior.DataSource = ds.Tables(1)
        cmbSuperior.DisplayMember = "NombreCompleto"
        cmbSuperior.ValueMember = "idEmpleado"

    End Sub

    Public Sub llenarTablaPersonal()

        Dim stringfuncion As String
        stringfuncion = ""
        Dim DS As New DataSet
        Dim DA As New SqlDataAdapter("SELECT p.idPersonal,e.NombreCompleto as Empleado,e.idEmpleado,e2.NombreCompleto as Superior,e2.IdEmpleado as idSuperior ,Case p.Funcion when '0' then 'ALMACENISTA' when '1' then 'CAPTURISTA' when '2' then 'ENCARGADO' End AS Funcion FROM CITRA_ERP.dbo.Personal p  left join  ctCITRAAUSL2012.dbo.empleados e on p.IDEMPLEADO  = e.IdEmpleado left join ctCITRAAUSL2012.dbo.empleados e2 on p.IDSUPERIOR=e2.idempleado order by idpersonal", variableconexion1)
        DA.Fill(DS)

        dgwPersonal.DataSource = DS.Tables(0)
        dgwPersonal.Columns(2).Visible = False
        dgwPersonal.Columns(4).Visible = False


    End Sub

    Private Sub frmPersonas_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        llenarCombo()
        llenarTablaPersonal()
        LblValoresAnterioresEditar.BackColor = Color.PaleVioletRed
        dgwPersonal.Columns(1).Width = 300
        dgwPersonal.Columns(3).Width = 300
        slideOpcionesPersonal.BackColor = Color.Black

        slideOpcionesPersonal.IsOpen = False
        slideAccionesPersonal.IsOpen = False
        slideNuevoPersonal.IsOpen = False
        slideReportesPersonal.IsOpen = False


    End Sub

    Private Sub ButtonItem7_Click(sender As Object, e As EventArgs)

        insertarActualizar()

    End Sub

    Public Sub insertarActualizar()


        Dim status As String

        status = "si"

        'Checamos que los campos esten completos para insercion de los valores

        If cmbEmpleado.SelectedValue.ToString.Equals("") Then

            status = "no"


        ElseIf cmbSuperior.SelectedValue.ToString.Equals("") Then

            status = "no"

        ElseIf cmbFuncion.Text.ToString.Equals("FUNCION") Then

            status = "no"

        End If


        'Si los campos fueron llenados correctamente se procede a realizar lo siguiente comparamos si se quiere modificar o insertar valores 

        If status.Equals("si") Then

            Dim fuction As String
            Dim valorFuncion As Integer

            fuction = cmbFuncion.SelectedItem.ToString

            If fuction = "ALMACENISTA" Then

                valorFuncion = 0

            End If

            If fuction = "CAPTURISTA" Then

                valorFuncion = 1

            End If

            If fuction = "ENCARGADO" Then

                valorFuncion = 2

            End If




            If lblModificarPersonal.Text.Equals("") Then

                Dim Query As New DataSet_Prod_ServTableAdapters.PersonalTableAdapter
                'Insertamos valores
                Query.Insert(cmbEmpleado.SelectedValue.ToString, cmbSuperior.SelectedValue.ToString, valorFuncion)
                PageSlider1.SelectedPage = PageSliderPage2
                llenarTablaPersonal()
                ToastNotification.Show(Me, "Insertado correctamente", My.Resources.checkHommer, 3000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)

            Else

                Dim DS As New DataSet
                Dim DA As New SqlDataAdapter("UPDATE Personal SET " & _
                                             "idEmpleado = " & cmbEmpleado.SelectedValue.ToString & "," & _
                                             "idSuperior = " & cmbSuperior.SelectedValue.ToString & "," & _
                                             "Funcion = " & valorFuncion.ToString & _
                                             "WHERE IDPERSONAL = " & lblModificarPersonal.Text & "", variableconexion1)
                DA.Fill(DS)
                PageSlider1.SelectedPage = PageSliderPage2
                llenarTablaPersonal()
                ToastNotification.Show(Me, "Modificado correctamente", My.Resources.checkHommer, 3000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)

            End If

        Else

            ToastNotification.Show(Me, "Completa todos los campos", My.Resources.cancelHommer, 3000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)

        End If


    End Sub

    Private Sub ButtonItem8_Click(sender As Object, e As EventArgs)

        limpiarCampos()
        llenarCombo()


    End Sub

    Public Sub Modificar()

        Dim fila As Integer
        fila = dgwPersonal.CurrentCell.RowIndex
        Dim valor As String
        valor = dgwPersonal.Rows(fila).Cells(0).Value.ToString()

        lblModificarPersonal.Text = dgwPersonal.Rows(fila).Cells(0).Value.ToString()
        LblValoresAnterioresEditar.Visible = True
        lblEtiquetaEmpleado.Visible = True
        lblEtiquetaSuperior.Visible = True
        lblEtiquetaFuncion.Visible = True
        lblMostrarNombreEmpleado.Visible = True
        lblMostrarSuperior.Visible = True
        lblMostrarFuncion.Visible = True

        lblMostrarNombreEmpleado.Text = dgwPersonal.Rows(fila).Cells(1).Value.ToString()
        lblMostrarSuperior.Text = dgwPersonal.Rows(fila).Cells(3).Value.ToString()
        lblMostrarFuncion.Text = dgwPersonal.Rows(fila).Cells(5).Value.ToString()



        cmbFuncion.Text = dgwPersonal.Rows(fila).Cells(5).Value.ToString()

        cmbEmpleado.SelectedValue = dgwPersonal.Rows(fila).Cells(2).Value.ToString()

        cmbSuperior.SelectedValue = dgwPersonal.Rows(fila).Cells(4).Value.ToString()

        PageSlider1.SelectedPage = PageSliderPage1

        ToastNotification.Show(Me, "Elige ahora los nuevos valores", My.Resources.ihommer, 5000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)



    End Sub

    Private Sub ButtonX2_Click(sender As Object, e As EventArgs) Handles ButtonX2.Click

        'Este es el boton del slide de la tabla para modificar los datos
        Modificar()
        llenarTablaPersonal()
        btncancelarbusqueda.Visible = False
        txtbuscar.Text = ""

    End Sub

    Private Sub ButtonX3_Click(sender As Object, e As EventArgs) Handles ButtonX3.Click
        'Este es el boton del slide de la tabla para mostrar los primeros datos
        PageSlider1.SelectedPage = PageSliderPage1
        limpiarCampos()
        llenarCombo()
        llenarTablaPersonal()
        btncancelarbusqueda.Visible = False
        txtbuscar.Text = ""

    End Sub

    Public Sub Buscar()

        Dim stringfuncion As String
        stringfuncion = ""
        Dim DS As New DataSet
        Dim DA As New SqlDataAdapter("SELECT p.idPersonal,e.NombreCompleto as Empleado, e.IdEmpleado,e2.NombreCompleto as Superior,e2.IdEmpleado as idSuperior,Case p.Funcion when '0' then 'ALMACENISTA' when '1' then 'CAPTURISTA' when '2' then 'ENCARGADO' End AS Funcion FROM CITRA_ERP.dbo.Personal p  join  ctCITRAAUSL2012.dbo.empleados e on p.IDEMPLEADO  = e.IdEmpleado join ctCITRAAUSL2012.dbo.empleados e2 on p.IDSUPERIOR=e2.idempleado and e2.NombreCompleto like  '%" & txtbuscar.Text.ToString & "%' union SELECT p.idPersonal, e.NombreCompleto as Empleado,e.IdEmpleado,  e2.NombreCompleto as Superior,e2.IdEmpleado as idSuperior, Case p.Funcion when '0' then 'ALMACENISTA' when '1' then 'CAPTURISTA' when '2' then 'ENCARGADO' End AS Funcion FROM CITRA_ERP.dbo.Personal p join  ctCITRAAUSL2012.dbo.empleados e on p.IDEMPLEADO  = e.IdEmpleado  and e.NombreCompleto like  '%" & txtbuscar.Text.ToString & "%' join ctCITRAAUSL2012.dbo.empleados e2 on p.IDSUPERIOR=e2.idempleado order by idpersonal", variableconexion1)
        DA.Fill(DS)

        dgwPersonal.DataSource = DS.Tables(0)
        dgwPersonal.Columns(2).Visible = False
        dgwPersonal.Columns(4).Visible = False

    End Sub

    Private Sub txtbuscar_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtbuscar.KeyPress
        Buscar()
        btncancelarbusqueda.Visible = True
    End Sub

    Private Sub btncancelarbusqueda_Click(sender As Object, e As EventArgs) Handles btncancelarbusqueda.Click

        llenarTablaPersonal()
        btncancelarbusqueda.Visible = False
        txtbuscar.Text = ""

    End Sub

    Public Sub limpiarCampos()

        cmbFuncion.Text = "FUNCION"

        lblModificarPersonal.Text = ""

        LblValoresAnterioresEditar.Visible = False
        lblEtiquetaEmpleado.Visible = False
        lblEtiquetaSuperior.Visible = False
        lblEtiquetaFuncion.Visible = False
        lblMostrarNombreEmpleado.Visible = False
        lblMostrarSuperior.Visible = False
        lblMostrarFuncion.Visible = False

        lblMostrarNombreEmpleado.Text = ""
        lblMostrarSuperior.Text = ""
        lblMostrarFuncion.Text = ""

    End Sub

    Private Sub ButtonItem42_Click(sender As Object, e As EventArgs) Handles ButtonItem42.Click
        insertarActualizar()
    End Sub

    Private Sub ButtonItem43_Click(sender As Object, e As EventArgs) Handles ButtonItem43.Click

        limpiarCampos()
        llenarCombo()

    End Sub

    Private Sub btnfolder_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub ButtonItem45_Click(sender As Object, e As EventArgs) Handles ButtonItem45.Click
        PageSlider1.SelectedPage = PageSliderPage2
        limpiarCampos()
    End Sub

    Private Sub btnMostrarOpciones_Click(sender As Object, e As EventArgs) Handles btnMostrarOpciones.Click
        slideOpcionesPersonal.IsOpen = True
    End Sub

    Private Sub ButtonItem7_Click_1(sender As Object, e As EventArgs) Handles ButtonItem7.Click
        slideOpcionesPersonal.IsOpen = False
    End Sub

    Private Sub ButtonItem24_Click(sender As Object, e As EventArgs) Handles ButtonItem24.Click
        slideReportesPersonal.IsOpen = False
    End Sub

    Private Sub ButtonItem39_Click(sender As Object, e As EventArgs) Handles ButtonItem39.Click
        slideReportesPersonal.IsOpen = True
    End Sub

    Private Sub ButtonItem23_Click(sender As Object, e As EventArgs) Handles ButtonItem23.Click
        slideAccionesPersonal.IsOpen = False
    End Sub

    Private Sub ButtonItem34_Click(sender As Object, e As EventArgs) Handles ButtonItem34.Click
        slideAccionesPersonal.IsOpen = True
    End Sub

    Private Sub btnCerrar_Click(sender As Object, e As EventArgs) Handles btnCerrar.Click
        slideNuevoPersonal.IsOpen = False
    End Sub

    Private Sub btnNuevo_Click(sender As Object, e As EventArgs) Handles btnNuevo.Click
        slideNuevoPersonal.IsOpen = True
    End Sub
End Class